package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.dal.base.constants.Constants;
import com.zbkj.dal.base.constants.UserLevelConstants;
import com.zbkj.dal.entity.system.SystemUserLevel;
import com.zbkj.dal.entity.user.User;
import com.zbkj.dal.entity.user.UserLevel;
import com.zbkj.dal.dao.UserLevelDao;
import com.zbkj.service.service.SystemConfigService;
import com.zbkj.service.service.SystemUserLevelService;
import com.zbkj.service.service.UserLevelService;
import com.zbkj.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * UserLevelServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class UserLevelServiceImpl extends ServiceImpl<UserLevelDao, UserLevel> implements UserLevelService {

    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
    private SystemUserLevelService systemUserLevelService;
    @Autowired
    private UserService userService;


    /**
     * 删除（通过系统等级id）
     *
     * @param levelId 系统等级id
     * @return Boolean
     */
    @Override
    public Boolean deleteByLevelId(Integer levelId) {
        LambdaUpdateWrapper<UserLevel> luw = Wrappers.lambdaUpdate();
        luw.set(UserLevel::getIsDel, true);
        luw.eq(UserLevel::getLevelId, levelId);
        luw.eq(UserLevel::getIsDel, false);
        return update(luw);
    }

    /**
     * 删除用户等级
     * @param userId 用户ID
     */
    @Override
    public Boolean deleteByUserId(Integer userId) {
        LambdaUpdateWrapper<UserLevel> luw = Wrappers.lambdaUpdate();
        luw.set(UserLevel::getIsDel, true);
        luw.eq(UserLevel::getUid, userId);
        luw.eq(UserLevel::getIsDel, false);
        return update(luw);
    }

    @Override
    public void userLevelUpgrade(User user) {
        String levelSwitch = systemConfigService.getValueByKey(UserLevelConstants.SYSTEM_USER_LEVEL_SWITCH);
        if (levelSwitch.equals(Constants.COMMON_SWITCH_CLOSE)) {
            return;
        }

        SystemUserLevel userLevel = systemUserLevelService.getByLevelId(user.getLevel());
        SystemUserLevel systemLevel = systemUserLevelService.getByDues(user.getDuesAmount());

        //充值后未达到级别
        if (systemLevel == null) {
            return;
        }

        if (ObjectUtils.nullSafeEquals(userLevel.getGrade(), systemLevel.getGrade())) {
            return;
        }


        UserLevel level = new UserLevel();
        level.setUid(user.getId());
        level.setLevelId(systemLevel.getId());
        level.setGrade(systemLevel.getGrade());

        userService.updateUserLevel(user.getId(), level.getLevelId());
        this.deleteByUserId(user.getId());
        this.save(level);
    }
}

